﻿Public Class frmGestionNotificaciones



    Private Sub frmGestionNotificaciones_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim fecha As Date = Date.Now.AddDays(nudDias.Value)


        Dim sError As String = ""

        sError = CargarVistaNotificaciones(CaserDataSet.VistaNotificaciones, fecha)

        If sError <> "" Then
            MessageBox.Show(sError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If


        sError = CargarVistaPoliza(CaserDataSet.VistaPolizas, fecha)

        If sError <> "" Then
            MessageBox.Show(sError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

        VistaNotificacionesBindingSource.Filter = "idLogin =" & Principal.rowlogin.id


    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()
    End Sub

    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        If dgNotifi.CurrentRow IsNot Nothing Then
            Dim sError As String = ""

            sError = CargarTabla(CaserDataSet.notificaciones)

            If sError <> "" Then
                MessageBox.Show(sError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
            CaserDataSet.notificaciones.Select("id=" & dgNotifi.CurrentRow.Cells(0).Value)(0).Delete()
            sError = GuardarTabla(CaserDataSet.notificaciones)
            If sError <> "" Then
                MessageBox.Show(sError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
                dgNotifi.Rows.Remove(dgNotifi.CurrentRow)

            End If

            If Me.MdiParent IsNot Nothing Then
                CType(Me.MdiParent, Principal).contarNotificaciones()
            End If
        End If
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim frmNuevaNotificación As frmNuevaNotificación
        frmNuevaNotificación = New frmNuevaNotificación
        frmNuevaNotificación.ShowDialog()
        If Me.MdiParent IsNot Nothing Then
            CType(Me.MdiParent, Principal).contarNotificaciones()
        End If

        Dim fecha As Date = Date.Now.AddDays(nudDias.Value)


        Dim sError As String = ""

        sError = CargarVistaNotificaciones(CaserDataSet.VistaNotificaciones, fecha)

        If sError <> "" Then
            MessageBox.Show(sError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub


    Private Sub nudDias_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudDias.ValueChanged
        Dim fecha As Date = Date.Now.AddDays(nudDias.Value)


        Dim sError As String = ""

        sError = CargarVistaNotificaciones(CaserDataSet.VistaNotificaciones, fecha)

        If sError <> "" Then
            MessageBox.Show(sError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

        sError = CargarVistaPoliza(CaserDataSet.VistaPolizas, fecha)

        If sError <> "" Then
            MessageBox.Show(sError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If dgNotifi.CurrentRow IsNot Nothing Then
            Dim sError As String = ""

            sError = CargarTabla(CaserDataSet.contacto)

            If sError <> "" Then
                MessageBox.Show(sError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
            If CaserDataSet.contacto.Select("id=" & CType(dgNotifi.CurrentRow.DataBoundItem.row, caserDataSet.VistaNotificacionesRow).idNotificacion).Length <> 0 Then

                If CType(Me.MdiParent, Principal).existeForm("frmGestionClientes") Then
                    Dim Formulario As Form

                    For Each Formulario In Me.MdiParent.MdiChildren
                        If Formulario.Name = "frmGestionClientes" Then
                            Formulario.Close()
                            Exit For
                        End If
                    Next Formulario
                End If
                Dim frmCliente As frmGestionClientes
                frmCliente = New frmGestionClientes
                frmCliente.MdiParent = Me.MdiParent
                Dim contactoRow As caserDataSet.contactoRow


                

                contactoRow = CaserDataSet.contacto.Select("id=" & CType(dgNotifi.CurrentRow.DataBoundItem.row, caserDataSet.VistaNotificacionesRow).idNotificacion)(0)

                frmCliente.Tag = contactoRow.Id & ";" & contactoRow.idPersona
                frmCliente.Show()




            Else
                MessageBox.Show("Esta notificacion no está asociada a ningún contacto", "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        End If
    End Sub

    Private Sub btnRenovar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRenovar.Click
        If dgPolizas.CurrentRow IsNot Nothing Then
            Dim sError As String
            Select Case dgPolizas.CurrentRow.Cells(3).Value
                Case "AUTO"
                    sError = CargarTabla(CaserDataSet._auto)
                    If sError <> "" Then
                        MessageBox.Show(sError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If

                    Dim autoRow As caserDataSet.autoRow = CaserDataSet._auto.Select("id=" & CType(dgPolizas.CurrentRow.DataBoundItem.row, caserDataSet.VistaPolizasRow).Id)(0)
                    autoRow.fechavec.AddYears(1)

                    sError = GuardarTabla(CaserDataSet._auto)
                    If sError <> "" Then
                        MessageBox.Show(sError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
                Case "HOGAR"
                    sError = CargarTabla(CaserDataSet.hogar)
                    If sError <> "" Then
                        MessageBox.Show(sError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
                    Dim hogarRow As caserDataSet.hogarRow = CaserDataSet.hogar.Select("id=" & CType(dgPolizas.CurrentRow.DataBoundItem.row, caserDataSet.VistaPolizasRow).Id)(0)
                    hogarRow.fechavec.AddYears(1)
                    sError = GuardarTabla(CaserDataSet.hogar)
                    If sError <> "" Then
                        MessageBox.Show(sError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
                Case "PYMECOMERCIO"
                    sError = CargarTabla(CaserDataSet.pymecomercio)
                    If sError <> "" Then
                        MessageBox.Show(sError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
                    Dim pymecomercioRow As caserDataSet.pymecomercioRow = CaserDataSet.pymecomercio.Select("id=" & CType(dgPolizas.CurrentRow.DataBoundItem.row, caserDataSet.VistaPolizasRow).Id)(0)
                    pymecomercioRow.fechavec.AddYears(1)
                    sError = GuardarTabla(CaserDataSet.pymecomercio)
                    If sError <> "" Then
                        MessageBox.Show(sError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If

                Case Else
                    sError = CargarTabla(CaserDataSet.poliza)
                    If sError <> "" Then
                        MessageBox.Show(sError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
                    Dim polizaRow As caserDataSet.polizaRow = CaserDataSet.poliza.Select("id=" & CType(dgPolizas.CurrentRow.DataBoundItem.row, caserDataSet.VistaPolizasRow).Id)(0)
                    polizaRow.fechavec = polizaRow.fechavec.AddYears(1)
                    sError = GuardarTabla(CaserDataSet.poliza)
                    If sError <> "" Then
                        MessageBox.Show(sError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If

            End Select
            nudDias_ValueChanged(nudDias, New EventArgs)
        End If
    End Sub

    Private Sub dtpFecha_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpFecha.ValueChanged
        Dim sError As String = ""
        Dim fecha = New Date(dtpFecha.Value.Year, dtpFecha.Value.Month + 1, 1)
        'MessageBox.Show(fecha.ToString)

        sError = CargarVistaNotificaciones(CaserDataSet.VistaNotificaciones, fecha)

        If sError <> "" Then
            MessageBox.Show(sError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

        sError = CargarVistaPoliza(CaserDataSet.VistaPolizas, fecha)

        If sError <> "" Then
            MessageBox.Show(sError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        VistaNotificacionesBindingSource.Filter = "idLogin =" & Principal.rowlogin.id & " and fecha >='" & New Date(dtpFecha.Value.Year, dtpFecha.Value.Month, 1).ToString & "'"
        VistaPolizasBindingSource.Filter = " fechavec  >= '" & New Date(dtpFecha.Value.Year, dtpFecha.Value.Month, 1).ToString & "'"

    End Sub
End Class